﻿(function () {
    var myApp = angular.module('MyControllers');

    // 意外处理所需控制器
    myApp.controller('unExpectedController', ['$scope', '$http', '$modal', 'unExpectedService', 'utils', function ($scope, $http, $modal, service, utils) {
        var srv = service.examPlanSrv;

        $scope.getList = function () {
            $scope.loadingState = true;
            srv.getList($scope.planType).success(function (data) {
                $scope.loadingState = false;
                $scope.list = data;
            });
        };

        $scope.official = function () {
            $scope.planType = 0;
            $scope.getList();
        };

        $scope.imatate = function () {
            $scope.planType = 1;
            $scope.getList();
        };

        $scope.chooseScene = function (model) {
            $modal.open({
                templateUrl: '/App/views/UnExpected/ChooseScene.html',
                controller: 'chooseScene',
                resolve: {
                    params: function () {
                        return model;
                    }
                }
            });
        }

        $scope.official();

        utils.setData({ modalCount: 0 });
        $scope.$root.$on('validateCode', function (event, data) {
            if (utils.getData('modalCount') < 1) {
                utils.setData({ modalCount: 1 });
                $modal.open({
                    templateUrl: '/App/views/UnExpected/ValidateCode.html',
                    controller: 'validateCode',
                    resolve: {
                        params: function () {
                            return data;
                        }
                    }
                });
            }
        });
    }]);

    // 选择场次的控制器及方法列表
    myApp.controller('chooseScene', ['$scope', '$routeParams', '$modalInstance', 'unExpectedService', 'utils', 'params', '$filter', function ($scope, $routeParams, $modalInstance, service, utils, params, $filter) {
        var srv = service.chooseScene;

        var methods = {
            getSceneList: function () {
                srv.getSceneList(params.examPlanInfoID).success(function (data) {
                    $scope.sceneList = data;
                });
            },
            save: function (model) {
                $scope.loadingState = true;
                srv.generateCode(params.examPlanInfoID, model.examSceneID).success(function (data) {
                    $scope.loadingState = false;

                    if (data.resultType == 'Success') {
                        $scope.close();

                        $scope.$emit('validateCode', { planInfo: params, scene: model, str: data.appendData });
                    } else {
                        utils.notify(data.message, 'warning');
                    }
                });
            },
            close: function () {
                utils.setData({ modalCount: 0 });
                $modalInstance.close(true);
            }
        };
        angular.extend($scope, methods);

        $scope.getSceneList();
    }]);

    // 验证码的控制器及方法列表
    myApp.controller('validateCode', ['$scope', '$routeParams', '$modalInstance', 'unExpectedService', 'utils', 'params', '$filter', function ($scope, $routeParams, $modalInstance, service, utils, params, $filter) {
        var srv = service.validateCode, planInfo = params.planInfo, scene = params.scene;
        $scope.requestCode = params.str;

        var methods = {
            save: function () {
                $scope.loadingState = true;
                srv.validCode($scope.authorizationCode).success(function (data) {
                    $scope.loadingState = false;

                    if (data.resultType == 'Success') {
                        $scope.loadingState = true;

                        var parameter = {
                            planId: planInfo.examPlanInfoID,
                            planName: planInfo.examPlanName,
                            sceneId: scene.examSceneID,
                            sceneName: scene.examSceneName
                        };

                        utils.encrypt(JSON.stringify(parameter)).success(function (data) {
                            $scope.close();

                            if (data.resultType == 'Success') {
                                window.location.href = '#/unExpected/control' + '?parameter=' + data.appendData;
                            } else {
                                utils.notify(data.message, 'warning');
                            }

                            $scope.loadingState = false;
                        });
                    } else {
                        utils.notify(data.message, 'warning');
                    }
                });
            },
            close: function () {
                utils.setData({ modalCount: 0 });
                $modalInstance.close(true);
            }
        };
        angular.extend($scope, methods);
    }]);

    // 意外控制需要的控制器及方法列表
    myApp.controller('unExpectedControlController', ['$scope', '$location', '$http', '$modal', 'unExpectedService', 'utils', function ($scope, $location, $http, $modal, service, utils) {
        var params, srv = service.controlSrv;

        var methods = {
            init: function () {
                $scope.loadingState = true;
                utils.decrypt($location.$$search.parameter).success(function (data) {
                    if (data.resultType == 'Success') {
                        params = JSON.parse(data.appendData);

                        $scope.baseInfo = params;
                        $scope.getRoomList();
                        $scope.getSubjectList();
                        $scope.getStatusList();

                        $scope.loadDetailGrid();
                    } else {
                        utils.notify(data.message, 'warning');
                    }

                    $scope.loadingState = false;
                });
            },
            getRoomList: function (subjectId) {
                srv.getRoomList(params.planId, params.sceneId, subjectId).success(function (data) {
                    data.unshift({ examRoomID: null, examRoomName: '全部考场' });

                    $scope.roomList = data;
                    $scope.curRoom = $scope.roomList[0];
                });
            },
            getSubjectList: function (roomId) {
                srv.getSubjectList(params.planId, params.sceneId, roomId).success(function (data) {
                    data.unshift({ examSubjectID: null, subjectName: '全部科目' });

                    $scope.subjectList = data;
                    $scope.curSubject = $scope.subjectList[0];
                });
            },
            getStatusList: function () {
                var data = [
                    { statusId: null, statusName: '全部状态' },
                    { statusId: 0, statusName: '未开考' },
                    { statusId: 1, statusName: '已登录' },
                    { statusId: 2, statusName: '已开考未交卷' },
                    { statusId: 3, statusName: '已交卷' }
                ];

                $scope.statusList = data;
                $scope.curStatus = $scope.statusList[0];
            },
            query: function () {
                var curRoom = $scope.curRoom;
                var curSubject = $scope.curSubject;
                var curStatus = $scope.curStatus;
                var parameter = $scope.curParams;

                $scope.loadDetailGrid(curRoom.examRoomID, curSubject.examSubjectID, curStatus.statusId, parameter);
            },
            checkAll: function () {
                var isCheck = $('.table :checkbox:first').is(':checked');

                $('.table :checkbox').not(':first').each(function () {
                    $(this).prop('checked', $(this).is(':disabled') ? false : isCheck);
                });

                $scope.isShowAdjust();
            },
            check: function (model) {
                var cbx = $('#cbx_' + model.examineeId);
                cbx.prop('checked', cbx.is(':disabled') ? false : !cbx.is(':checked'));

                $scope.isShowAdjust();
            },
            save: function () {
                var selList = [];
                angular.forEach($scope.list, function (data, index, array) {
                    var cbx = $('#cbx_' + data.examineeId);

                    if (cbx.is(':checked') && !cbx.is(':disabled')) {
                        selList.push(data.examineeId);
                    }
                });

                if (selList.length <= 0) {
                    utils.notify('请选择一项进行调整时间操作！', 'warning');
                } else {
                    var adjustTime = $('.datetimepicker:first').val();
                    if (!adjustTime) {
                        utils.notify('请选择一个需要调整的时间点！', 'warning');
                    } else {
                        $scope.loadingState = true;
                        srv.saveTime(params.planId, params.sceneId, selList, adjustTime, $scope.curComments).success(function (resultData) {
                            $scope.loadingState = false;

                            if (resultData.resultType == 'Success') {
                                // 生成后刷新当前页面
                                $scope.query();
                                utils.notify('成功调整时间！', 'success');
                            } else {
                                utils.notify(resultData.message, 'warning');
                            }
                        });
                    }
                }
            },
            applyRetake: function (model) {
                $modal.open({
                    templateUrl: '/App/views/UnExpected/ApplyRetake.html',
                    controller: 'applyRetake',
                    resolve: {
                        params: function () {
                            return { model: model, baseInfo: params };
                        }
                    }
                });
            },
            loadDetailGrid: function (roomId, subjectId, statusId, parameter) {
                srv.getDetailList(params.planId, params.sceneId, roomId, subjectId, statusId, parameter).success(function (data) {
                    $scope.list = data;
                    $scope.isAdjust = false;

                    // 测试申请重考
                    // $scope.list[0].Syste = 3;
                });
            },
            quit: function () {
                $scope.isAdjust = false;
            },
            isShowAdjust: function () {
                var selList = [];
                angular.forEach($scope.list, function (data, index, array) {
                    var cbx = $('#cbx_' + data.examineeId);

                    if (cbx.is(':checked') && !cbx.is(':disabled')) {
                        selList.push(data.examineeId);
                    }
                });

                $scope.isAdjust = selList.length > 0;
            }
        };

        angular.extend($scope, methods);

        $scope.init();

        $scope.$root.$on('loadDetailGrid', function () {
            $scope.query();
        });
    }]);

    // 申请重考的控制器及方法列表
    myApp.controller('applyRetake', ['$scope', '$routeParams', '$modalInstance', 'unExpectedService', 'utils', 'params', '$filter', function ($scope, $routeParams, $modalInstance, service, utils, params, $filter) {
        var srv = service.retakeSrv, model = params.model, baseInfo = params.baseInfo;

        var methods = {
            save: function () {
                var adjustTime = $('.datetimepicker:last').val();
                if (!adjustTime) {
                    utils.notify('请选择一个重考的时间点！', 'warning');
                } else {
                    $scope.loadingState = true;
                    srv.applyRetake(baseInfo.planId, baseInfo.sceneId, model.examineeId, adjustTime, $scope.curComments).success(function (data) {
                        $scope.loadingState = false;

                        if (data.resultType == 'Success') {
                            // 生成后刷新当前页面
                            $scope.$emit('loadDetailGrid');
                            utils.notify('成功申请重考！', 'success');
                            $scope.close();
                        } else {
                            utils.notify(data.message, 'warning');
                        }
                    });
                }
            },
            close: function () {
                utils.setData({ modalCount: 0 });
                $modalInstance.close(true);
            }
        };
        angular.extend($scope, methods);
    }]);
})();